package offer;

import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 剑指 Offer 34. 二叉树中和为某一值的路径
 * </p>
 *
 * @author xiaozuolin
 * @date 2021/9/24 9:37
 */
public class Offer34 {
    LinkedList<List<Integer>> res = new LinkedList<>();
    LinkedList<Integer> path = new LinkedList<>();

    public List<List<Integer>> pathSum(TreeNode root, int target) {
        recur(root, target);
        return res;
    }

    public void recur(TreeNode root, int target) {
        if (root == null) {
            return;
        }
        path.add(root.val);
        target -= root.val;
        if (target == 0 && root.left == null && root.right == null) {
            res.add(new LinkedList<>(path));
        }
        recur(root.left, target);
        recur(root.right, target);
        path.removeLast();
    }
}
